home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / language / dl_exsrc.zoo / splitpth.c < prev    next >
C/C++ Source or Header  |  1994-07-03  |  2KB  |  80 lines

  1. #include <string.h>
  2. #include <ctype.h>
  3. #include "extras.h"
  4.  
  5. char *_splitpath(src, drive, path, file, ext)
  6.   const char *src;
  7.   char *drive, *path, *file, *ext;
  8. {
  9.   register const char *s = src;
  10.   register char *t;
  11.   int rooted;            /* detect "a:filename.ext" */
  12.  
  13.   /* Get drive section.  We also recognize and translate the MiNT
  14.      device "/dev/a" as "a:". */
  15.   if (isalpha(*s) && s[1] == ':') {
  16.     rooted = 1;
  17.     if (drive) {
  18.       t = drive;
  19.       *t++ = *s;
  20.       *t++ = ':';
  21.       *t = '\0';
  22.     }
  23.     s += 2;
  24.   } else if (strncmp(s, "/dev/", 5) == 0 && isalpha(s[5])) {
  25.     rooted = 1;
  26.     if (drive) {
  27.       t = drive;
  28.       *t++ = s[5];
  29.       *t++ = ':';
  30.       *t = '\0';
  31.     }
  32.     s += 6;
  33.   } else {
  34.     rooted = 0;
  35.     if (drive)
  36.       *drive = '\0';
  37.   }
  38.  
  39.   /* Get path section.  We translate all /'s to \'s. */
  40.   t = strrpbrk(s, "/\\");
  41.   if (!t || t == s) {
  42.     if (path && (*s == '/' || *s == '\\' || rooted)) {
  43.       path[0] = '\\';
  44.       path[1] = '\0';
  45.     }
  46.     if (t)
  47.       s++;
  48.   } else {
  49.     if (path) {
  50.       strncpy(path, s, (int)(t - s));
  51.       path[(int)(t - s)] = '\0';
  52.     }
  53.     s = t + 1;
  54. #if 1    /* maybe don't do this */
  55.     if (path) {
  56.       for (t = path; *t; t++)
  57.     if (*t == '/')
  58.       *t = '\\';
  59.     }
  60. #endif
  61.   }
  62.  
  63.   /* Get filename and extension.  Note that only the last '.' is
  64.      recognized; the filename "fred.tar.Z" would split into "fred.tar"
  65.      and "Z". */
  66.   t = strrchr(s, '.');
  67.   if (ext) {
  68.     if (t)
  69.       strcpy(ext, t + 1);
  70.     else
  71.       ext[0] = '\0';
  72.   }
  73.   if (file) {
  74.     strncpy(file, s, (int)(t - s));
  75.     file[(int)(t - s)] = '\0';
  76.   }
  77.  
  78.   return src;
  79. }
  80.